<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="单据号" prop="requisitionPo">
        <el-input v-model="queryParams.requisitionPo" placeholder="请输入单据号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="请购计划名称" prop="requisitionName">
        <el-input v-model="queryParams.requisitionName" placeholder="请输入请购计划名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="请购人员" prop="requisitionPerson">
        <el-input v-model="queryParams.requisitionPerson" placeholder="请输入请购人员" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="日期" prop="requisitionDate">
        <el-date-picker clearable v-model="queryParams.requisitionDate" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['requisition:requisition:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['requisition:requisition:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['requisition:requisition:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['requisition:requisition:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="requisitionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="requisitionId" />
      <el-table-column label="单据号" align="center" prop="requisitionPo" />

      <el-table-column label="请购计划名称" align="center" prop="requisitionName">
        <template slot-scope="scope">
          <router-link :to="'/requisition-table/requisition/' + scope.row.requisitionId" class="link-type">
            <span>{{ scope.row.requisitionName }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="供应商" align="center" prop="supplier.supplierName" width="200px"/>
      <el-table-column label="请购人员" align="center" prop="requisitionPerson" />
      <el-table-column label="采购类型" align="center" prop="requisitionType">
        <template slot-scope="scope">
          <span v-if="scope.row.requisitionType == 0">
            <el-tag type="success">普通采购</el-tag>
          </span>
          <span v-if="scope.row.requisitionType == 1">
            <el-tag type="danger">紧急采购</el-tag>
          </span>

        </template>
      </el-table-column>
      <el-table-column label="日期" align="center" prop="requisitionDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.requisitionDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="auditStatus">
        <template slot-scope="scope">
          <span v-if="scope.row.auditStatus == 0">
            <el-tag type="danger">未审核</el-tag>
          </span>
          <span v-if="scope.row.auditStatus == 1">
            <el-tag type="success">已审核</el-tag>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="审核人" align="center" prop="auditPerson" />
      <el-table-column label="审核时间" align="center" prop="auditTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.auditTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <span v-if="scope.row.auditStatus == 0">
            <el-button size="mini" type="text" icon="el-icon-circle-check" @click="handleGoReqion(scope.row)"
              v-hasPermi="[' requisition:requisition:audit']">审核下单</el-button>
          </span>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['requisition:requisition:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['requisition:requisition:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 下单操作 -->
    <el-dialog :title="title" :visible.sync="openRequsition" width="960px" append-to-body>
      <el-form :model="queryParam" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="采购单" prop="requisitionId">
          <el-input v-model="queryParam.requisitionId" placeholder="请输入采购单" clearable @keyup.enter.native="handleQuery"
            readonly />
        </el-form-item>
        <el-form-item label="单据号" prop="requisitionPo">
          <span style="padding-right:55px;font-size:15px;">{{ Reqion.requisitionPo }}</span>
        </el-form-item>
        <el-form-item label="请购人员" prop="requisitionPerson">
          <span style="padding-right:55px;font-size:15px;">{{ Reqion.requisitionPerson }}</span>
        </el-form-item>

        <el-form-item label="日期" prop="requisitionDate">
          <span style="padding-right:55px;font-size:15px;">{{ Reqion.requisitionDate }}</span>
        </el-form-item>

        <el-form-item label="税率" prop="requisitionTableTax">
          <el-input v-model="Reqion.requisitionTableTax" placeholder="请输入税率" />
        </el-form-item>
        <el-form-item label="供应商" prop="supplierId">
          <el-select v-model="Reqion.supplierId" placeholder="请选择供应商">
            <el-option v-for="item in supplierList"
                       :key="item.supplierName"
                       :label="item.supplierName"
                       :value="item.supplierId">
            </el-option>
          </el-select>
        </el-form-item>


      </el-form>



      <el-table v-loading="loading" :data="tableList" @selection-change="handleChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="采购数据id" align="center" prop="requisitionTableId" />
        <el-table-column label="存货编号" align="center" prop="requisitionOrderNo" />
        <el-table-column label="存货名称" align="center" prop="requisitionTableName" />
        <el-table-column label="规格型号" align="center" prop="requisitionTableSpeion" />
        <el-table-column label="单位" align="center" prop="requisitionTableUnit" />
        <el-table-column label="数量" align="center" prop="requisitionTableNum" />
        <el-table-column label="单价" align="center" prop="requisitionTablePrice" />
        <el-table-column label="税合计" align="center" prop="requisitionTableTotal" />
        <el-table-column label="税率" align="center" prop="requisitionTableTax" />
        <el-table-column label="需求日期" align="center" prop="requisitionTableDate" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.requisitionTableDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>


      </el-table>

      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="Addhandle" :disabled="multiple" v-hasPermi="['requisition:requisition:audit']">确
          定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改请购计划对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="960px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="请购计划名称" prop="requisitionName">
          <el-input v-model="form.requisitionName" placeholder="请输入请购计划名称" />
        </el-form-item>
        <el-form-item label="请购人员" prop="requisitionPerson" >
          <el-input v-model="form.requisitionPerson" placeholder="请输入请购人员"/>
        </el-form-item>
        <el-form-item label="日期" prop="requisitionDate">
          <el-date-picker clearable v-model="form.requisitionDate" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="供应商" prop="supplierId">
          <el-select v-model="form.supplierId" placeholder="请选择供应商" @change="suppliertableLists">
            <el-option v-for="item in supplierList" :key="item.supplierName" :label="item.supplierName"
              :value="item.supplierId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择商品" prop="supplierId">
          <el-select v-model="form.supplierTableId" placeholder="请选择商品" @change="GoodsLists" >
            <el-option v-for="item in supTableList" :key="item.supplierTableName" :label="item.supplierTableName"
              :value="item.supplierTableId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="税率" prop="requisitionTableTax">
          <el-input v-model="form.requisitionTableTax" placeholder="请输入税率" />
        </el-form-item>

        <el-table v-loading="loading" :data="GoodsList" @selection-change="handleGoodsChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200px">
            <template slot-scope="scope">
              <el-input-number v-model="scope.row.requisitionNum" @change="handleNum" :min="1" :max="100000" label="描述文字"></el-input-number>

          </template>
      </el-table-column>
          <el-table-column label="规格型号" align="center" prop="supplierGoodsImage" width="150">
            <template slot-scope="scope">
              <image-preview :src="scope.row.supplierGoodsImage" :width="50" :height="50" />
            </template>
          </el-table-column>
          <el-table-column label="规格型号" align="center" prop="supplierGoodsName" />
          <el-table-column label="克重" align="center" prop="supplierGoodsWeight" />
          <el-table-column label="纱支" align="center" prop="supplierGoodsSaori" />
          <el-table-column label="颜色" align="center" prop="supplierGoodsColor" />
          <el-table-column label="幅宽" align="center" prop="supplierGoodsWidth" />
          <el-table-column label="印花风格" align="center" prop="supplierGoodsStyle" />
          <el-table-column label="单价" align="center" prop="supplierGoodsPrice" />
          <el-table-column label="库存" align="center" prop="supplierGoodsNum" />
          <el-table-column label="备注" align="center" prop="remark" />

        </el-table>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { auditRequisition, listRequisition, getRequisition, delRequisition, addRequisition, updateRequisition } from "@/api/requisition/requisition";
import { listTable } from "@/api/requisition/table";
import { listSupplier } from "@/api/supplier/supplier";
import { listSuppliergoods } from "@/api/supplier/suppliergoods";
import { listSuppliertable,getSuppliertable } from "@/api/supplier/suppliertable";

export default {
  name: "Requisition",
  data() {
    return {

      goodsData: {},
      listOrder: [],
      goods: [],
      GoodsList: [],
      supTableList: [],
      tableList: [],
      Reqion: {
        requisitionTableTax: '',
        requisitionId: '',
        requisitionDate: '',
        requisitionPerson: '',
        supplierId: '',
      },
      openRequsition: false,//窗口
      // 遮罩层
      loading: true,
      // 选中数组
      supplierList: [],
      ids: [],
      rows: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 请购计划表格数据
      requisitionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 采购订单查询参数
      queryParam: {
        pageNum: 1,
        pageSize: 10,
        requisitionId: null,

      },
      requisitionData: {},
      list: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        requisitionPo: null,
        requisitionName: null,
        requisitionPerson: null,
        requisitionType: null,
        requisitionDate: null,
      },
      GoReqion: {
        requisitionTableTax: '',
        requisitionId: '',
        requisitionDate: '',
        requisitionPerson: '',
        supplierId: '',
      },
      queryParamtable: { supplierId: null },
      queryParamgoods: { supplierTableGoods: null },
      // 表单参数
      form: {},
      suptable:[],
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleNum(value){

    },
    //商品
    suppliertableLists() {
      this.queryParamtable.supplierId = this.form.supplierId
      console.log(this.queryParamtable)
      listSuppliertable(this.queryParamtable).then(response => {
        this.supTableList = response.rows;
      });
    },

    /** 查询商品 */
    GoodsLists() {
      this.queryParamgoods.supplierTableId = this.form.supplierTableId
      const supplierTableId=this.form.supplierTableId
      listSuppliergoods(this.queryParamgoods).then(response => {
        this.GoodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      getSuppliertable(supplierTableId).then(response => {
        this.suptable = response.data
      })
    },
    //供应商分类
    supplierLists() {
      listSupplier(null).then(response => {
        this.supplierList = response.rows;
      });
    },
    /** 批量添加按钮操作 */
    Addhandle(row) {
      this.requisitionData.requisitionIds = this.GoReqion.requisitionId;
      this.requisitionData.purchaseDate = this.Reqion.requisitionDate;
      this.requisitionData.supplierIds = this.Reqion.supplierId;
      this.requisitionData.purchasePerson = this.Reqion.requisitionPerson;
      this.requisitionData.requisitionTableTax = this.Reqion.requisitionTableTax;

      this.requisitionData.orderList = this.rows
      console.log(this.requisitionData);
      auditRequisition(this.requisitionData).then(response => {
        this.$modal.msgSuccess("审核操作成功");
        this.openRequsition = false;
        this.getList();
      })

    },
    ReqionList(requisitionId) {
      getRequisition(requisitionId).then(response => {
        this.Reqion = response.data
      })
    },
    getRequisition() {
      listTable(this.queryParam).then(response => {
        this.tableList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //审核下单操作
    handleGoReqion(row) {
      this.GoReqion.requisitionId = row.requisitionId;
      this.openRequsition = true;
      this.queryParam.requisitionId = row.requisitionId
      this.getRequisition();
      const requisitionId = row.requisitionId
      this.ReqionList(requisitionId);

      this.supplierLists();
      this.title = '采购下单窗口';

    },
    /** 查询请购计划列表 */
    getList() {
      this.loading = true;
      listRequisition(this.queryParams).then(response => {
        this.requisitionList = response.rows;

        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        requisitionId: null,
        requisitionPo: null,
        requisitionName: null,
        requisitionPerson: null,
        requisitionType: null,
        requisitionDate: null,
        auditStatus: null,
        auditPerson: null,
        auditTime: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleChange(selection) {
      this.rows = selection.map(item => item);
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // 多选框选中数据
    handleGoodsChange(selection) {
      this.goods = selection.map(item => item)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.requisitionId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.supplierLists();
      this.suppliertableLists();
      this.title = "添加请购计划";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const requisitionId = row.requisitionId || this.ids
      getRequisition(requisitionId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改请购计划";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.requisitionId != null) {
            updateRequisition(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {

            // 调用获取当前日期的方法加四位随机数  赋值表单中的项目编号
            this.goodsData.requisitionPo = this.getProjectNum() + Math.floor(Math.random() * 100)
            this.goodsData.requisitionName = this.form.requisitionName;
            this.goodsData.requisitionPerson = this.form.requisitionPerson;
            this.goodsData.requisitionDate = this.form.requisitionDate;
            this.goodsData.supplierId = this.form.supplierId;
            this.goodsData.supplierTableName=this.suptable.supplierTableName;
            this.goodsData.requisitionTableTax = this.form.requisitionTableTax;
            this.goodsData.listOrder = this.goods
           console.log(this.goodsData)
            addRequisition(this.goodsData).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleOptionClick(supplierTableId) {
      getSuppliertable(supplierTableId).then(response => {
        this.suptable = response.data
      })

      // let opt= {};
      // opt= this.supTableList.find((item)=>{
      //     return item.value === value;
      // });
      // this.goodsData.supplierTableName = opt;


    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const requisitionIds = row.requisitionId || this.ids;
      this.$modal.confirm('是否确认删除请购计划编号为"' + requisitionIds + '"的数据项？').then(function () {
        return delRequisition(requisitionIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('requisition/requisition/export', {
        ...this.queryParams
      }, `requisition_${new Date().getTime()}.xlsx`)
    },
    // 获取当前日期的方法
    getProjectNum() {
      const projectTime = new Date() // 当前中国标准时间
      // const Year = projectTime.getFullYear() // 获取当前年份 支持IE和火狐浏览器.
      const Month = projectTime.getMonth() + 1 // 获取中国区月份
      const Day = projectTime.getDate() // 获取几号
      var CurrentDate = 0
      if (Month >= 10) { // 判断月份和几号是否大于10或者小于10
        CurrentDate += Month
      } else {
        CurrentDate += '0' + Month
      }
      if (Day >= 10) {
        CurrentDate += Day
      } else {
        CurrentDate += '0' + Day
      }
      return CurrentDate
    },
  }
};
</script>
